Ext.define('AmpleHarvest.objects.forms.irtCommon.requestInformation.requestCommon.requestFrequency', 
	{
		extend : 'Ext.form.Panel',
		alias : 'widget.ampleharvest-objects-forms-irtcommon-requestinformation-requestcommon-requestfrequency',
		requires :
			[
				"Ext.form.field.Text", 
				"Ext.form.FieldSet",
				"Ext.form.field.Date", 
				'Ext.layout.container.VBox',
				'Ext.layout.container.HBox',
			],
		itemId : 'requestimport_panel',
		
		changeFrequency: function(changedFrequency){
			switch(changedFrequency){
				case 'once':
					this.frequencyRecurring(false);
					this.frequencyCustom(false);
					break;
				case 'custom':
					this.frequencyRecurring(true);
					this.frequencyCustom(true);
					break;
				default:
					this.frequencyRecurring(true);
					this.frequencyCustom(false);
					break;
			}
		},

		frequencyRecurring: function(bool_show){
			if(bool_show){
					this.stopDateField.enable();
					this.stopDateField.show();
			}else{
					this.stopDateField.disable();
					this.stopDateField.hide();
			}
		},
		
		frequencyCustom: function(bool_show){
			if(bool_show){
					this.customDescription.enable();
					this.customDescription.show();
			}else{
					this.customDescription.disable();
					this.customDescription.hide();
			}
		},

		initComponent : function()
		{
			var dateToday = new Date();
			var dateNextYear = new Date(dateToday.getFullYear() + 1, dateToday.getMonth(), dateToday.getDate() );
			var DUE_DATE = 'Due Date';
			var START_DATE = 'Start Date';
			var STOP_DATE = 'Stop Date';
			
			this.requestFrequencyVBox = Ext.create(
			"Ext.form.FieldSet",
			{
				title : "Frequency",
				labelWidth : 70,
				width : 275,
				collapsible : false,
				defaultType: 'textfield',
			    layout : { type : 'vbox', align : 'left' }
				}
			);
			
		//-------------------- Frequency --------------------//

			this.onceRadio = Ext.create('Ext.form.field.Radio',
				{
					boxLabel: 'Once', 
		        	name: 'frequency',  
		        	inputValue: 'once', 
		        	checked: true,
		        	handler: function(checkbox, checked) {
	            		if(checked){
	            			this.up('ampleharvest-objects-forms-irtcommon-requestinformation-requestcommon-requestfrequency').changeFrequency(checkbox.inputValue);
	            		}
	            	}
				}
			);
			this.dailyRadio = Ext.create('Ext.form.field.Radio',
				{
		        	boxLabel: 'Daily', 
		        	name: 'frequency', 
		        	inputValue: 'daily',
		        	handler: function(checkbox, checked) {
	            		if(checked){
	            			this.up('ampleharvest-objects-forms-irtcommon-requestinformation-requestcommon-requestfrequency').changeFrequency(checkbox.inputValue);
	            		}
	            	}
				}
			);
			this.weeklyRadio = Ext.create('Ext.form.field.Radio',
				{
					boxLabel: 'Weekly',
		        	name: 'frequency', 
		        	inputValue: 'weekly',
		        	handler: function(checkbox, checked) {
	            		if(checked){
	            			this.up('ampleharvest-objects-forms-irtcommon-requestinformation-requestcommon-requestfrequency').changeFrequency(checkbox.inputValue);
	            		}
	            	} 
				}
			);
			this.monthlyRadio = Ext.create('Ext.form.field.Radio',
				{
					boxLabel: 'Monthly', 
		        	name: 'frequency', 
		        	inputValue: 'monthly',
		        	handler: function(checkbox, checked) {
	            		if(checked){
	            			this.up('ampleharvest-objects-forms-irtcommon-requestinformation-requestcommon-requestfrequency').changeFrequency(checkbox.inputValue);
	            		}
	            	} 
				}
			);
			this.customRadio = Ext.create('Ext.form.field.Radio',
				{
					boxLabel: 'Custom', 
		        	name: 'frequency', 
		        	inputValue: 'custom',
		        	handler: function(checkbox, checked) {
	            		if(checked){
	            			this.up('ampleharvest-objects-forms-irtcommon-requestinformation-requestcommon-requestfrequency').changeFrequency(checkbox.inputValue);
	            		}
	            	} 
				}
			);
			
			this.requestFrequencyLeftHBox = Ext.create(
				'Ext.Panel', 
				{ 
					border : false,
					layout : { type : 'hbox', align : 'middle' },
					items : [
						{
						    xtype : 'container',
						    defaultType : 'radiofield',
						    defaults : { flex : 1 },
						    layout : { type : 'vbox' },
						    items : [this.onceRadio, this.dailyRadio, this.weeklyRadio]
						}
					]
				}
			);
		
		this.requestFrequencyRightHBox = Ext.create(
			'Ext.Panel', 
			{ 
				border : false,
				layout : { type: 'hbox', align: 'middle' },
				items: [
					{
					    xtype : 'container',
					    defaultType : 'radiofield',
					    defaults : { flex : 1 },
					    layout : { type : 'vbox' },
					    items : [this.monthlyRadio, this.customRadio]
					}
				]
			}
		);
		
		this.requestFrequency = Ext.create(
			"Ext.form.Panel",
			{
				border: false,
				width : 275,
				items : [
				{
		            xtype : 'radiogroup',
		            allowBlank : false,
				    defaultType : 'container',
				    defaults : { flex : 1 },
				    layout : { type : 'hbox' },
				    items :  
				    [
				     this.requestFrequencyLeftHBox,
				     this.requestFrequencyRightHBox
				    ]
				}]
			}
		);
		
		//-------------------- Dates --------------------//
		this.startDateField = Ext.create(
			'Ext.form.DateField',
			{
	            fieldLabel: START_DATE,
	            labelAlign	: 'right',
	            name: 'start_date', //PHP Column label
	            hidden: false,
	        	allowBlank : false,
	            value: dateToday,
	            altFormats: 'Y-m-d H:i:s'
			}
		);
		
		this.stopDateField = Ext.create(
			'Ext.form.DateField',
			{
	            fieldLabel: STOP_DATE,
	            labelAlign	: 'right',
	            name: 'stop_date', //PHP Column label
	            hidden: true,
	            disabled: true,
	        	allowBlank : false,
	            value: dateNextYear,
	            altFormats: 'Y-m-d H:i:s'
            }
		);
		
		this.dueDateField = Ext.create(
			'Ext.form.DateField',
			{
	            fieldLabel: DUE_DATE,
	            labelAlign	: 'right',
	            name: 'due_date', //PHP Column label
	            hidden: false,
	        	allowBlank : false,
//	            value: null,
	            altFormats: 'Y-m-d H:i:s'
			}
		);

		this.requestFrequencyDateVBox = Ext.create('Ext.Panel', 
		{
		    border: false,
			defaultType: 'datefield',
		    layout: 
		    {
		        type: 'vbox',
		        align: 'left'
		    },
		    items: [this.startDateField, this.stopDateField, this.dueDateField ]
		});
		

		
		this.customDescription = Ext.create("Ext.form.FieldSet",
			{
				title		: "Custom Description",
				collapsible	: false,
				labelWidth	: 70,
				width		: 250,
				itemId		: 'customdescription_fieldset',
				hidden		: true,
	            disabled: true,
				items: [
					{
						border		: false,
						itemId			: 'customdescription_items',
					    layout			: { type : 'vbox' },
					    align			: 'center',
					    items: 
					    [
					        {
					            xtype: 'text',
						        text: "Please provide specific,\ndetailed instructions for\nthis custom import.\n \nSpecific Instructions:"
					        },
					        {
					        	xtype     : 'textarea',
					        	itemId	  : 'customdescription_textarea',
								grow      : true,
							    //TODO: Add REST SERVICE
								name: 'custom_frequency', //PHP Column label
								anchor    : '100%',
								width		: 225,
								allowBlank	: false
					        },
					    ]
					}
				]
			}
		);
		
		this.requestFrequencyVBox.add(this.requestFrequency);	
		this.requestFrequencyVBox.add(this.requestFrequencyDateVBox);
		this.requestFrequencyVBox.add(this.customDescription);	
		
		this.items = [ this.requestFrequencyVBox ]
		
	    this.callParent();
	}
});